package com.tsn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsn.common.Result;
import com.tsn.entity.Comment;
import com.tsn.entity.Forum;
import com.tsn.mapper.CommentMapper;
import com.tsn.mapper.ForumMapper;
import com.tsn.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 论坛 服务实现类
 * </p>
 *
 * @author tsn
 * @since 2021-09-07
 */
@Service
public class ForumServiceImpl extends ServiceImpl<ForumMapper, Forum> implements ForumService {
    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Result listForums() {

        return null;
    }

    @Override
    public Result addForums(Forum forum, Comment comment) {
        forumMapper.insert(forum);
        Integer id = forum.getId();
        comment.setType(0);
        comment.setArticleId(id);
        commentMapper.insert(comment);
//        Comment comment = new Comment();
//        comment.set
        return Result.success("添加成功!",null);
    }

    @Override
    public Result searchForumByWords(String word) {
        QueryWrapper<Forum> forumQueryWrapper = new QueryWrapper<>();
        forumQueryWrapper.like("forum_title","%"+word+"%");
        List<Forum> forums = forumMapper.selectList(forumQueryWrapper);
        if(forums == null)
            return Result.failure("暂无数据");
        return Result.success("查询成功",forums);
    }

    @Override
    public int addCommentNumber(Integer forumId) {

        Forum forum = forumMapper.selectById(forumId);
        forum.setCommentNumber(forum.getCommentNumber()+1);
        forumMapper.updateById(forum);
        return 1;
    }
}
